package JavaBean;

import com.ktjy.pojo.Persons;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class DOM4JReadExample {
    public static void main(String[] args) {
        SAXReader reader = new SAXReader();
        try {
            // 读取XML文件并获取根元素
            Document document = reader.read(new File("D:\\gongju\\idea\\wj\\mavenproject\\untitled\\src\\main\\resources\\tmp.xml"));
            Element rooElement = document.getRootElement();

            List<Persons> personList = new ArrayList<>();

            // 遍历XML中的 "stu" 元素
            for (Object child : rooElement.elements("stu")) {
                if (child instanceof Element) {
                    Element element = (Element) child;

                    // 通过反射创建Persons类的实例
                    Constructor<Persons> constructor = Persons.class.getConstructor();
                    Persons person = constructor.newInstance();

                    // 遍历 "stu" 元素中的 "pro" 元素
                    for (Object proObj : element.elements("pro")) {
                        if (proObj instanceof Element) {
                            Element proElement = (Element) proObj;
                            String name = proElement.attributeValue("name");
                            String val = proElement.attributeValue("val");

                            if (name!= null && val!= null) {
                                // 通过反射设置Person对象的属性值
                                Field field = Persons.class.getDeclaredField(name);
                                field.setAccessible(true);

                                Class<?> fieldType = field.getType();
                                if (fieldType == String.class) {
                                    field.set(person, val);
                                } else if (fieldType == int.class) {
                                    try {
                                        field.set(person, Integer.parseInt(val));
                                    } catch (NumberFormatException e) {
                                        System.err.println("无法将值 " + val + " 转换为整数类型，设置 " + name + " 属性失败");
                                        e.printStackTrace();
                                    }
                                } else if (fieldType == double.class) {
                                    try {
                                        field.set(person, Double.parseDouble(val));     
                                    } catch (NumberFormatException e) {
                                        System.err.println("无法将值 " + val + " 转换为双精度类型，设置 " + name + " 属性失败");
                                        e.printStackTrace();
                                    }
                                } else if (fieldType == boolean.class) {
                                    try {
                                        field.set(person, Boolean.parseBoolean(val));
                                    } catch (Exception e) {
                                        System.err.println("无法将值 " + val + " 转换为布尔类型，设置 " + name + " 属性失败");
                                        e.printStackTrace();
                                    }
                                } else {
                                      System.err.println("不支持的字段类型：" + fieldType.getName() + "，设置 " + name + " 属性失败");
                                }
                            } else {
                                System.err.println("name或val属性值为空，跳过设置 " + name + " 属性");
                            }
                        }
                    }
                    personList.add(person);
                }
            }

            // 打印出所有的Persons对象
            for (Persons person : personList) {
                System.out.println(person);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
